package com.ruoyi.mes.service.impl;

import java.io.IOException;
import java.util.Comparator;
import java.util.List;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.mes.domain.*;
import com.ruoyi.mes.mapper.*;
import com.ruoyi.mes.utils.LongMenUtils;
import com.ruoyi.mes.utils.QualityInspectionUtils;
import com.ruoyi.mes.vo.product.ProStockInhouVo;
import com.ruoyi.mes.vo.qualityinspection.QualityInspectionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import com.ruoyi.mes.service.IFinishInhouService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 完工入库Service业务层处理
 *
 * @author LongAndHao
 * @date 2023-05-17
 */
@Service
public class FinishInhouServiceImpl implements IFinishInhouService {
    @Autowired
    private HouInfoMapper houInfoMapper;

    @Autowired
    private ProStockMapper proStockMapper;

    @Autowired
    private ProInfoMapper proInfoMapper;

    @Autowired
    private FinishInhouMapper finishInhouMapper;

    @Autowired
    private LocatorInfoMapper locatorInfoMapper;

    /**
     * 查询完工入库
     *
     * @param id 完工入库主键
     * @return 完工入库
     */
    @Override
    public FinishInhou selectFinishInhouById(Long id) {
        return finishInhouMapper.selectFinishInhouById(id);
    }

    /**
     * 查询完工入库列表
     *
     * @param finishInhou 完工入库
     * @return 完工入库
     */
    @Override
    public List<FinishInhou> selectFinishInhouList(FinishInhou finishInhou) {
        List<FinishInhou> finishInhous = finishInhouMapper.selectFinishInhouList(finishInhou);
        if (finishInhous.size() != 0) {//处理返回产品信息
            for (FinishInhou inhous : finishInhous) {
                ProInfo proInfo = proInfoMapper.selectProInfoById(inhous.getProId());
                inhous.setProCode(proInfo.getProCode());
                inhous.setProName(proInfo.getProName());
            }
        }
        return finishInhous;
    }

    /**
     * 新增完工入库
     *
     * @param finishInhou 完工入库
     * @return 结果
     */
    @Override
    public int insertFinishInhou(FinishInhou finishInhou) {
        finishInhou.setCreateTime(DateUtils.getNowDate());
        return finishInhouMapper.insertFinishInhou(finishInhou);
    }

    /**
     * 修改完工入库
     *
     * @param finishInhou 完工入库
     * @return 结果
     */
    @Override
    public int updateFinishInhou(FinishInhou finishInhou) {
        finishInhou.setUpdateTime(DateUtils.getNowDate());
        return finishInhouMapper.updateFinishInhou(finishInhou);
    }

    /**
     * 批量删除完工入库
     *
     * @param ids 需要删除的完工入库主键
     * @return 结果
     */
    @Override
    public int deleteFinishInhouByIds(Long[] ids) {
        return finishInhouMapper.deleteFinishInhouByIds(ids);
    }

    /**
     * 删除完工入库信息
     *
     * @param id 完工入库主键
     * @return 结果
     */
    @Override
    public int deleteFinishInhouById(Long id) {
        return finishInhouMapper.deleteFinishInhouById(id);
    }


    /**
     * 入库
     * @param proStockInhouVo
     * @return
     */
    @Override
    @Transactional
    public AjaxResult inHou(ProStockInhouVo proStockInhouVo) {
        FinishInhou finishInhou = finishInhouMapper.selectFinishInhouById(proStockInhouVo.getId());//入库记录
        ProInfo proInfo = proInfoMapper.selectProInfoById(finishInhou.getProId());//产品信息

        //判断库房剩余容量是否充足
        HouInfo houInfo = houInfoMapper.selectHouInfoByHouId(proStockInhouVo.getHouId());
        if ( houInfo.getHouRemainCapacity() < finishInhou.getInhouQuantity()) {
            return AjaxResult.error("库存剩余容量不足!");
        }

//        //调取龙门,根据库位进行放料
//        LongMenUtils.LongMenPutWithLocator(proStockInhouVo.getLocatorInfoId());

        //更新库房剩余容量
        houInfo.setHouRemainCapacity(houInfo.getHouRemainCapacity() - finishInhou.getQualifiedNum());
        houInfoMapper.updateHouInfo(houInfo);

        //更新库位
        LocatorInfo locatorInfo = locatorInfoMapper.selectLocatorInfoById(proStockInhouVo.getLocatorInfoId());
        if (locatorInfo.getSerialCode() == null || StringUtils.isBlank(locatorInfo.getSerialCode())){//空库位
            locatorInfo.setSerialCode(proInfo.getProCode());
        } else if (!locatorInfo.getSerialCode().equals(proInfo.getProCode())) {
            return AjaxResult.error("物品不匹配!");
        }
        locatorInfo.setRemainCapacity(locatorInfo.getRemainCapacity() - finishInhou.getQualifiedNum());
        locatorInfoMapper.updateLocatorInfo(locatorInfo);

        //更新产品库存
        ProStock proStock = proStockMapper.selectMtrStockByMtrCode(proStockInhouVo.getHouId(), proInfo.getProCode());

        if (proStock == null) {//若产品库存没有存放过该产品，则新增
            ProStock stock = new ProStock();
            stock.setHouId(proStockInhouVo.getHouId());
            stock.setProCode(proInfo.getProCode());
            stock.setProName(proInfo.getProName());
            stock.setProType(proInfo.getProType());
            stock.setProQuantity(finishInhou.getQualifiedNum());
            proStockMapper.insertProStock(stock);

        } else {
            //更新产品库存
            proStock.setProQuantity(proStock.getProQuantity() + finishInhou.getQualifiedNum());
            proStockMapper.updateProStock(proStock);
        }
        //更新入库状态
        finishInhou.setStatus(1L);
        finishInhou.setInhouName(SecurityUtils.getLoginUser().getUser().getNickName());
        finishInhou.setInhouTime(proStockInhouVo.getInhouTime());
        finishInhouMapper.updateFinishInhou(finishInhou);

        return AjaxResult.success("入库成功!");
    }

    /**
     * 质检
     * @param id 完工入库id
     * @return
     */
    @Override
    public AjaxResult qualityInspection(Long id) throws IOException {
        //判断质检是否通过
        QualityInspectionVo qualityInspectionVo = QualityInspectionUtils.qualityInspection();
        if (qualityInspectionVo.getIs_pass() == false){
            return AjaxResult.success("质检未通过!",qualityInspectionVo);
        }

        //合格后合格数加1
        FinishInhou finishInhou = finishInhouMapper.selectFinishInhouById(id);
        finishInhou.setQualifiedNum(finishInhou.getQualifiedNum() + 1);
        finishInhouMapper.updateFinishInhou(finishInhou);
        return AjaxResult.success("质检通过!",qualityInspectionVo);
    }
}
